package cn.org.qary.controller.yaokaiing.back;

import cn.org.qary.entity.News;
import cn.org.qary.service.trait.yaokaiing.NewsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller("back_NewsController")
@RequestMapping("back_news")
public class NewsController {
    @Autowired
    NewsService ns;

    int rows=3;
    @RequestMapping("show/{page}")
    public String findAll(@PathVariable(required = false) Integer page,Model model){
        if (page==null) page=1;
        Page<Object> pages = PageHelper.startPage(page, rows);
        List<News> list=ns.selectAll();
        model.addAttribute("news",list);

        model.addAttribute("pages",pages);
        return "manage/news";
    }

    @RequestMapping("add")
    public String addnews(News news){
        ns.insert(news);
        return "redirect:show/1";
    }

    @RequestMapping("del/{id}")
    public String delnews(@PathVariable Integer id){
        ns.delete(id);
        return "redirect:/back_news/show/1";
    }

    @RequestMapping("showOne/{id}")
    public String showOne(@PathVariable Integer id,Model model){
        News news=ns.selectById(id);
        model.addAttribute("news",news);
        return "manage/news-modify";
    }

    @RequestMapping("update")
    public String updatenews(News news){
        ns.update(news);
        return "redirect:show/1";
    }
}
